#include <memory>
#include <unistd.h>
#include <string>
#include <cstdio>
#include "UdpServer.hpp"
#include "comm.h"

void Usage(const std::string& proc)
{
    std::cout << "Usage:\n\t" << proc << " local_port\n" << std::endl;
}

std::string OnMessage(std::string message)
{
    std::string ret = message;
    ret += "[haha, got you!]";
    return ret;
}

std::string excuteCommand(std::string message)
{
    std::cout << "get a message: " << message << std::endl;
    FILE* fp = popen(message.c_str(), "r");
    if(fp == nullptr)
    {
        std::cout << "Unknown Command" << std::endl;
    }

    std::string response;
    char buffer[1024];
    while(true)
    {
        char* s = fgets(buffer, sizeof(buffer), fp);
        if(s == nullptr) break;
        else response += s;
    }
    pclose(fp);
    return response;
}

int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        Usage(argv[0]);
        exit(usageError);
    }
    uint16_t port = std::stoi(argv[1]);
    std::unique_ptr<UdpServer> usvr = std::make_unique<UdpServer>(port);
    usvr->Init();
    usvr->Start();
    
    return 0;
}